#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>

#define handlle_error(cmd,result){  \
    if (result < 0)                 \
    {                               \
        perror(cmd);                \
        return -1;                  \
    }                               \
}      

int main(int argc, char const *argv[])
{
    struct sockaddr_in server_addr;
    int sock_fd,tmp_result,pid;
    memset(&server_addr,0,sizeof(server_addr));

    // 1.赋值
    server_addr.sin_family = AF_INET;
    inet_pton(AF_INET,"192.168.10.150",&server_addr.sin_addr);
    server_addr.sin_port = htons(6666);

    // 2.创建socker
    sock_fd = socket(AF_INET,SOCK_STREAM,0);
    handlle_error("socket",sock_fd);

    // 3. 绑定地址
    tmp_result = bind(sock_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
    handlle_error("bind",tmp_result);

    // 4. 开启监听
    listen(sock_fd,1024);

    printf("服务端开启完成  等待连接\n");
    pid = fork();
    if (pid == 0)
    {
        sleep(200);
    }else{
        fgetc(stdin); 
    }
    
    close(sock_fd);
    printf("服务端关闭\n");

    return 0;
}
